﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WebcamControl;
using System.IO;
using Microsoft.Expression.Encoder.Devices;
using Microsoft.Expression.Encoder;

namespace Presentacion.Controles
{
    /// <summary>
    /// Interaction logic for Camara.xaml
    /// </summary>
    public partial class Camara : UserControl
    {
        public Camara()
        {
            InitializeComponent();
            string vidPath = @"C:\VideoClips";

            if (Directory.Exists(vidPath) == false)
            {
                Directory.CreateDirectory(vidPath);
            }

            var a = EncoderDevices.FindDevices(EncoderDeviceType.Video);

            WebCamCtrl.VideoDevice = EncoderDevices.FindDevices(EncoderDeviceType.Video)[0].Name;
            WebCamCtrl.AudioDevice = EncoderDevices.FindDevices(EncoderDeviceType.Audio)[0].Name;

            WebCamCtrl.VideoDirectory = vidPath;
            WebCamCtrl.VidFormat = VideoFormat.mp4;
            WebCamCtrl.FrameRate = 30;
            WebCamCtrl.FrameSize = new System.Drawing.Size(1280, 720);

            inicializarCamara();

        }

        public void inicializarCamara()
        {
            try
            {
                WebCamCtrl.StartCapture();
            }
            catch (SystemErrorException ex)
            {
                //MessageBox.Show("Device is in use by another application" + ex.Message);
            }
        }

        public void detenerCamara()
        {
            try
            {
                WebCamCtrl.StopCapture();
            }
            catch (SystemErrorException ex)
            {
                MessageBox.Show("Device is in use by another application" + ex.Message);
            }
        }

        public void empezarGrabacion()
        {
            WebCamCtrl.StartRecording();
        }

        public void detenerGrabacion()
        {
            WebCamCtrl.StopRecording();
        }
    }
}
